package zhu;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AdminPanel extends JFrame {
    private StudentManagementDialogs studentManagementDialogs = new StudentManagementDialogs();
    private CourseManagementDialogs courseManagementDialogs = new CourseManagementDialogs();

    public AdminPanel() {
        setTitle("管理员面板");
        setSize(800, 400);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        // 创建左侧面板
        JPanel leftPanel = new JPanel();
        leftPanel.setLayout(new BoxLayout(leftPanel, BoxLayout.Y_AXIS));
        leftPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); // 减少边距

        // 创建右侧面板
        JPanel rightPanel = new JPanel();
        rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.Y_AXIS));
        rightPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10)); // 减少边距

        // 创建带有标题的面板
        JPanel studentPanel = createTitledBorderPanel("学生管理");
        JPanel coursePanel = createTitledBorderPanel("课程管理");

        // 管理学生的按钮
        JButton addStudentButton = new JButton("添加学生");
        JButton deleteStudentButton = new JButton("删除学生");
        JButton modifyStudentButton = new JButton("修改学生");
        JButton queryStudentButton = new JButton("查询学生");

        // 管理课程的按钮
        JButton addCourseButton = new JButton("添加课程");
        JButton deleteCourseButton = new JButton("删除课程");
        JButton modifyCourseButton = new JButton("修改课程");
        JButton queryCourseButton = new JButton("查询课程");

        // 将按钮添加到对应的分类面板
        studentPanel.add(addStudentButton);
        studentPanel.add(Box.createVerticalStrut(5)); // 添加垂直间隔
        studentPanel.add(deleteStudentButton);
        studentPanel.add(Box.createVerticalStrut(5));
        studentPanel.add(modifyStudentButton);
        studentPanel.add(Box.createVerticalStrut(5));
        studentPanel.add(queryStudentButton);

        coursePanel.add(addCourseButton);
        coursePanel.add(Box.createVerticalStrut(5));
        coursePanel.add(deleteCourseButton);
        coursePanel.add(Box.createVerticalStrut(5));
        coursePanel.add(modifyCourseButton);
        coursePanel.add(Box.createVerticalStrut(5));
        coursePanel.add(queryCourseButton);

        // 将分类面板添加到左侧面板
        leftPanel.add(studentPanel);
        leftPanel.add(Box.createVerticalStrut(10)); // 添加垂直间隔
        leftPanel.add(coursePanel);

        // 管理问题的按钮
        JPanel questionPanel = createTitledBorderPanel("问题管理");
        JButton addQuestionButton = new JButton("添加问题");
        JButton deleteQuestionButton = new JButton("删除问题");
        JButton modifyQuestionButton = new JButton("修改问题");
        JButton queryQuestionButton = new JButton("查询问题");

        // 查询学生答案记录的按钮
        JButton queryAnswerRecordsButton = new JButton("查询学生答案记录");

        // 退出登录按钮
        JButton logoutButton = new JButton("退出登录");

        // 将按钮添加到问题管理面板
        questionPanel.add(addQuestionButton);
        questionPanel.add(Box.createVerticalStrut(5));
        questionPanel.add(deleteQuestionButton);
        questionPanel.add(Box.createVerticalStrut(5));
        questionPanel.add(modifyQuestionButton);
        questionPanel.add(Box.createVerticalStrut(5));
        questionPanel.add(queryQuestionButton);

        // 将问题管理面板和查询、退出按钮添加到右侧面板
        rightPanel.add(questionPanel);
        rightPanel.add(Box.createVerticalStrut(10));
        rightPanel.add(queryAnswerRecordsButton);
        rightPanel.add(Box.createVerticalStrut(5));
        rightPanel.add(logoutButton);

        // 创建分割面板，将左侧和右侧面板放在一起
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, leftPanel, rightPanel);
        splitPane.setDividerLocation(300);
        splitPane.setResizeWeight(0.5);

        // 将分割面板添加到主框架
        add(splitPane);

        // 退出登录按钮动作监听器
        logoutButton.addActionListener(e -> {
            new Login().setVisible(true);
            dispose();
        });
        // 退出登录按钮动作监听器
        logoutButton.addActionListener(e -> {
            new Login().setVisible(true);
            dispose();
        });
        // 退出登录按钮动作监听器
        logoutButton.addActionListener(e -> {
            new Login().setVisible(true);
            dispose();
        });

        // 添加按钮的动作监听器
        addStudentButton.addActionListener(e -> studentManagementDialogs.openAddStudentDialog(this));
        deleteStudentButton.addActionListener(e -> studentManagementDialogs.openDeleteStudentDialog(this));
        modifyStudentButton.addActionListener(e -> studentManagementDialogs.openModifyStudentDialog(this));
        queryStudentButton.addActionListener(e -> studentManagementDialogs.openQueryStudentDialog(this));

        addCourseButton.addActionListener(e -> courseManagementDialogs.openAddCourseDialog(this));
        deleteCourseButton.addActionListener(e -> courseManagementDialogs.openDeleteCourseDialog(this));
        modifyCourseButton.addActionListener(e -> courseManagementDialogs.openModifyCourseDialog(this));
        queryCourseButton.addActionListener(e -> courseManagementDialogs.openQueryCourseDialog(this));
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new AdminPanel().setVisible(true);
        });
    }


// 创建带有标题的面板
private JPanel createTitledBorderPanel(String title) {
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    panel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), title));
    return panel;
}
}